package com.eascs.finance.admin.server.manager.impl;

import java.util.Date;

import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.finance.admin.common.entity.ArticleItemEntity;
import com.eascs.finance.admin.common.tool.PinYin2Abbreviation;
import com.eascs.finance.admin.common.vo.ArticleItemQueryVO;
import com.eascs.finance.admin.server.manager.IArticleItemManager;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Repository
public class ArticleItemManagerImpl extends BaseDao implements IArticleItemManager {



	private final Logger logger = LoggerFactory.getLogger(ArticleItemManagerImpl.class);

	@Override
	public String addArticleItem(ArticleItemEntity articleItemEntity) {
		User user = UserHolder.getUser();
		articleItemEntity.setUpdateTime(new Date());
		articleItemEntity.setCreateTime(new Date());
		if (user != null) {
			articleItemEntity.setUpdater(user.getCname());
			articleItemEntity.setCreator(user.getCname());
		}

		int rank = findMaxRank();
		if (articleItemEntity.getRank()==0) {
			articleItemEntity.setRank(rank + 1);
		}
		
		if (StringUtil.isEmpty(articleItemEntity.getItemUrl())) {
			articleItemEntity.setItemUrl(PinYin2Abbreviation.cn2py(articleItemEntity.getItemName()));
		}
		articleItemEntity.setParentId("0");
		articleItemEntity.setStatus(1);
		articleItemEntity.setIsLock(0);
		boolean flag = this.saveEntity(articleItemEntity);
		if (flag == true) {
			return articleItemEntity.getId();
		} else {
			return null;
		}
	}

	@Override
	public boolean updateArticleItem(ArticleItemEntity articleItemEntity) {
		User user = UserHolder.getUser();
		articleItemEntity.setUpdateTime(new Date());
		if (user != null) {
			articleItemEntity.setUpdater(user.getCname());
		}

		if (StringUtil.isEmpty(articleItemEntity.getItemUrl())) {
			articleItemEntity.setItemUrl(PinYin2Abbreviation.cn2py(articleItemEntity.getItemName()));
		}
		return this.updateEntity(articleItemEntity);
	}

	@Override
	public PageResult<Object[]> findArticleItemInfo(ArticleItemQueryVO vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());

		StringBuilder sql = new StringBuilder();
		sql.append("select article.id,article.item_name,article.publish_time,article.rank,article.publisher,item.item_name as status from article_item article"
				+ " left join datadic_item item on article.status=item.item_code and dd_no=1 "
				+ " where article.is_lock=0 ");

		// 栏目名称
		if (StringUtil.isNotBlank(vo.getItemName())) {
			sql.append(" and article.item_name like ?");
			sqlPageQuery.addParam("%"+vo.getItemName()+"%");

		}
		// 栏目类型
		if (vo.getStatus()!=null) {
			sql.append(" and article.status= ?");
			sqlPageQuery.addParam(vo.getStatus());
		}
		
		sql.append(" order by article.rank desc");
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> page = this.findByPage(sqlPageQuery);
		return page;
	}

	@Override
	public boolean deleteArticleItem(String id) {

		return this.excuteSql("update article_item set is_lock=1 where id=? ",id);
	}

	@Override
	public boolean updateArticleItemStatus(String id, int status) {
		User user = UserHolder.getUser();
		String sql="update article_item set status=? where id=? " ;
		if(status==2){
			
			sql="update article_item set status=?, publisher=?, publish_time=? where id=?  " ;
		   return this.excuteSql(sql,status,user.getCname(),new Date(),id);
		}
		return this.excuteSql(sql,status,id);
	}

	@Override
	public ArticleItemEntity findArticleItemById(String id) {
		
		return this.getById(ArticleItemEntity.class, id);
	}

	@Override
	public Integer findMaxRank() {
		
		Integer num =this.findInteger("select max(rank) from article_item");
		if(num==null){
			num=0;
		}
		return num;
	}

	@Override
	public Integer findSameRankNum(String id, int rank) {
	   if(StringUtil.isNotBlank(id)){
		   return this.findInteger("select count(1) from article_item where rank=? and id<>? ",rank,id);
	   }else{
		return this.findInteger("select count(1) from article_item where rank=?",rank);
	   }
	}


}
